package engine.newUi
{
	import engine.data.ModelLocator;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	
	import newx.com.config.StaticIni;

	public class WorldBossRankLogic
	{
		// 9金色 10 粉 11浅蓝
		protected const COLOR_TABLE	:Array	=	["","#ffffff'>","#1eff00'>","#0070dd'>","#a335ee'>","#ff8000'>","#e5cc80'>","#ffff00'>","#ff0000'>","#00ff06'>","#ff07ff'>","#14a8ed'>"];
		private static var _self	:WorldBossRankLogic;
		private var _ui			:WorldBossRankUI;
		private var _model			:ModelLocator;
		private var _infoVector	:Vector.<RankVo>;
		
		private const MIN_NUM:Number =0.005;
		private const MIN_SHOW_NUM:Number =0.01;
		public function WorldBossRankLogic()
		{
		}
		public static function getInstance():WorldBossRankLogic
		{
			if(_self == null)
			{
				_self = new WorldBossRankLogic();
			}
			return _self;
		}
		public function  Init():void
		{
			_ui 	= WorldBossRankUI.getInstance();
			_infoVector=new Vector.<RankVo>();
			_model	= ModelLocator.getInstance();
			_model	.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_WORLD_BOSS_TOP_LIST),getRankInfo);
			_model	.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_WORLD_BOSS_PLAYER_SCORE),getSlefInfo);
			
		}
		public function	BeforeShow():void
		{
			_ui.mouseEnabled = _ui.mouseChildren=false;
			_ui.panle.mouseEnabled =_ui.panle.mouseChildren= false;

		}
		public function	AfterHide():void
		{
			clearAll();
		}
		private function clearAll():void
		{
			_ui.selfinfo.htmlText = "";
			_ui.selfname.htmlText = "";
			_ui.selfNum	.htmlText =	"";
			_ui.nameText.htmlText = "";
			_ui.infoText.htmlText = "";
			_ui.numText .htmlText = "";
		
		}
		private function getRankInfo(e:CustomMsgEvent =null):void
		{
			
			var infoArr:Array = [];
			infoArr	= e.pList;
			saveRankVo(infoArr);
			showStr();
		}
		private function getSlefInfo(e:CustomMsgEvent =null):void
		{
			var vo:RankVo = new RankVo(e.pList[4],e.pList[0],e.pList[1],setNumber(e.pList[2],2),e.pList[3]);
			if(e.pList.length==0 || vo.total<=0)
			{
				_ui.selfname.htmlText =StaticIni.getSwfLangStr("UIString_1000114");
				_ui.selfinfo.text ="";
			}
			else
			{
				_ui.selfname.htmlText =byVoSetInfoStr(vo,1);
				_ui.selfinfo.htmlText =byVoSetInfoStr(vo,2);
				_ui.selfNum	.htmlText =byVoSetInfoStr(vo,3);
			}
		}
		private function saveRankVo(arr:Array):void
		{
			_infoVector=new Vector.<RankVo>();
			var  index:int =0;
			for(var i:int=0;i<arr.length;i+=4)
			{
				index++;
				_infoVector.push(new RankVo(index,arr[i],arr[i+1],setNumber(arr[i+2],2),arr[i+3]));
			}
		}
		/**
		 * 获取类型的字符串    
		 * @param vo
		 * @param type   1名字段 2 伤害值段 3 击杀数段
		 * @return 
		 * 
		 */		
		private function byVoSetInfoStr(vo:RankVo,type:int):String
		{
			var ColorID:int;
			switch(vo.index)
			{
				case 1:
					ColorID =4;
					break;
				case 2:
					ColorID =5;
					break;
				case 3:
					ColorID =7;
					break;
				default:
					ColorID =1;
					break;
			}
			var infoArr:Array =[];
			var strID	:String;
			switch(type)
			{
				case 1:
					infoArr = [vo.index,vo.name];
					strID	= "UIString_1000112";
					break;
				case 2:
					infoArr = [vo.total,vo.percent];
					strID	= "UIString_1000113";
					break;
				case 3:
					infoArr = [vo.num];
					strID	= "UIString_1000115";
					break;
			}
			var str:String = link_color(StaticIni.getSwfLangStrVar(strID,infoArr),ColorID);
			return str;
		}
		private function link_color(str:String,color:int):String
		{
			if(color < 1 || color >8)
			{
				color	=	1;
			}
			return ("<font color ='"+COLOR_TABLE[color]+str+"</font>");
		}
		private function showStr():void
		{
			_ui.nameText.htmlText ="";
			_ui.infoText.htmlText ="";
			_ui.numText	.htmlText ="";
			for(var i:String  in _infoVector)
			{
				var namestr:String	= byVoSetInfoStr(_infoVector[i],1);
				var infostr:String	= byVoSetInfoStr(_infoVector[i],2);
				var numstr :String	= byVoSetInfoStr(_infoVector[i],3);
				_ui.nameText.htmlText += namestr+"<br>";
				_ui.infoText.htmlText += infostr+"<br>";
				_ui.numText	.htmlText += numstr +"<br>";
			}
		}
		/**
		 *设置Number格式 如 4.25， 0， 
		 * @param num 数字
		 * @param val 保留位数
		 * @return 
		 * 
		 */		
		private function setNumber(num:Number,val:int):Number
		{
 			if(num>0 && num<MIN_NUM)
			{
				return MIN_SHOW_NUM;
			}
			var numStr:String =  num.toFixed(val);
			return Number(numStr);
		}
		
	}
	
}
class RankVo
{
	public var index	:int;	  //排名
	public var name	:String   //名字
	public var total	:Number;  //总伤害
	public var percent	:Number;	  //伤害百分比	
	public var num		:int;	  //击杀数
	public function RankVo(index:int,name:String,total:Number,percent:Number,num:int)
	{
		this.index		= index;
		this.name 		= name;
		this.total		= total;
		this.percent	= percent;
		this.num		= num;
	}
}